package com.vhly.apb;

import android.content.Context;
import android.content.SharedPreferences;
import com.vhly.remoteapi.YahooClient;
import org.scribe.model.Token;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-13
 * Time: 23:07:30
 * Email: vhly@163.com
 */
public class APBYahooClient extends YahooClient {

    private Context context;

    public APBYahooClient(Context ct) {
        context = ct;
    }

    @Override
    public boolean loadToken() {
        boolean ret = false;
        if (context != null) {
            SharedPreferences sp = context.getSharedPreferences("apbpref", Context.MODE_WORLD_WRITEABLE);
            String tk, tks;
            tk = sp.getString("tk", null);
            tks = sp.getString("tks", null);
            if (tk != null && tks != null) {
                accessToken = new Token(tk, tks);
                String s = sp.getString("oaei", null);
                if (s != null) {
                    accessToken.addOther(OAEI, s);
                }

                s = sp.getString("oei", null);
                if (s != null) {
                    accessToken.addOther(OEI, s);
                }

                s = sp.getString("osh", null);
                if (s != null) {
                    accessToken.addOther(OSH, s);
                }

                s = sp.getString("guid", null);
                if (s != null) {
                    accessToken.addOther(GUID, s);
                }

                s = sp.getString("gent", null);
                if (s != null) {
                    accessToken.addOther(GENT, s);
                }
                ret = true;
            }
        }
        return ret;
    }

    @Override
    public boolean saveToken() {
        boolean ret = false;
        if (context != null) {
            SharedPreferences sp = context.getSharedPreferences("apbpref", Context.MODE_WORLD_WRITEABLE);
            if (accessToken != null) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("tk", accessToken.getToken());
                editor.putString("tks", accessToken.getSecret());
                String s = accessToken.getOther(OAEI);
                if (s != null) {
                    editor.putString("oaei", s);
                }
                s = accessToken.getOther(OEI);
                if (s != null) {
                    editor.putString("oei", s);
                }

                s = accessToken.getOther(OSH);
                if (s != null) {
                    editor.putString("osh", s);
                }

                s = accessToken.getOther(GUID);
                if (s != null) {
                    editor.putString("guid", s);
                }

                s = accessToken.getOther(GENT);
                if (s != null) {
                    editor.putString("gent", s);
                }
                editor.commit();
            }
        }
        return ret;
    }

    public boolean accessTokenIsExpired() {
        boolean bret = false;
        String gent = accessToken.getOther(GENT);
        String oei = accessToken.getOther(OEI);
        if (gent != null && oei != null) {
            long gt = Long.parseLong(gent);
            long ed = Long.parseLong(oei) * 1000;
            long ct = System.currentTimeMillis();
            bret = (ct - gt) > ed;
        }
        return bret;
    }
}
